﻿using System;
using System.Collections.Generic;

namespace Sasa.QualityTools.CodeAnalysis.Common
{
    public class TraceLogger : ILogger
    {
        private static TraceLogger current;

        private List<string> logs;


        public static TraceLogger Current
        {
            get
            {
                if (current == null)
                {
                    current = new TraceLogger();
                }
                return current;
            }
        }

        public string Logs
        {
            get
            {
                return String.Join(Environment.NewLine, logs);
            }
        }


        private TraceLogger()
        {
            logs = new List<string>();
        }


        public void Info(string format, params object[] args)
        {
            var message = Text.Format(format, args);
            logs.Add(Text.Format("[info] {0} {1}", DateTimeOffset.Now, message));
        }

        public void Warn(string format, params object[] args)
        {
            var message = Text.Format(format, args);
            logs.Add(Text.Format("[warn] {0} {1}", DateTimeOffset.Now, message));
        }

        public void Error(string format, params object[] args)
        {
            var message = Text.Format(format, args);
            logs.Add(Text.Format("[error] {0} {1}", DateTimeOffset.Now, message));
        }

        public void Error(Exception exception, string format, params object[] args)
        {
            var message = Text.Format(format, args);
            logs.Add(Text.Format("[error] {0} {1}", DateTimeOffset.Now, message));
            logs.Add(Text.Format(exception));
        }

        public void Clear()
        {
            logs.Clear();
        }
    }
}
